use flate2::write::GzEncoder;
use flate2::Compression;

/// Encode a protobuf message to bytes, optionally gzip-compressed.
pub fn encode_body<T: prost::Message>(use_gzip: bool, data: &T) -> (Vec<u8>, Option<&'static str>) {
    let mut bytes = Vec::with_capacity(1024);
    prost::Message::encode(data, &mut bytes).expect("encode protobuf");
    if use_gzip {
        let mut e = GzEncoder::new(Vec::with_capacity(bytes.len() / 2 + 32), Compression::default());
        let _ = std::io::Write::write_all(&mut e, &bytes);
        let gz = e.finish().unwrap_or_default();
        (gz, Some("gzip"))
    } else {
        (bytes, None)
    }
}


